package com.briup.app02.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.app02.bean.CategoryExample;
import com.briup.app02.bean.Product;
import com.briup.app02.bean.ProductExample;
import com.briup.app02.bean.extend.ProductExtend;
import com.briup.app02.dao.ProductMapper;
import com.briup.app02.dao.extend.ProductExtendMapper;
import com.briup.app02.service.IProductService;
import com.briup.app02.utils.CustomerException;

@Service
public class ProductServiceImpl implements IProductService {
	@Autowired
	private ProductMapper productMapper;
	@Autowired
	private ProductExtendMapper productExtendMapper;
	@Override
	public void saeOrUpdate(Product product) throws CustomerException {
		// TODO Auto-generated method stub
		if(product.getId()!=null) {
			productMapper.updateByPrimaryKey(product);
		}else {
			ProductExample example=new ProductExample();
			example.createCriteria().andNameEqualTo(product.getName());
			List<Product>list=productMapper.selectByExample(example);
			if(list.size()>0) {
				throw new CustomerException("同名产品已存在");
			}
			productMapper.insert(product);
		}
	}
		@Override
		public List<Product>query(String name,String status,Long productCategoryId){
			ProductExample example=new ProductExample();
			
			ProductExample.Criteria criteria=example.createCriteria();
			if (name!=null) {
				criteria.andNameLike("%"+name+"%");
			}
			if(status!=null) {
				criteria.andStatusEqualTo(status);
			}
			if(productCategoryId!=null) {
				criteria.andProductCategoryIdEqualTo(productCategoryId);
			}
			List<Product>list=productMapper.selectByExample(example);
			return list;
		}
		
		
		
	
	/*@Override
	public List<Product> query(String name, String status, Long productCategoryId) {
		// TODO Auto-generated method stub
		return null;
	}*/
	@Override
	public void deleteById(long id) throws CustomerException {
		// TODO Auto-generated method stub
		Product Product=productMapper.selectByPrimaryKey(id);
		if(Product==null){
			throw new CustomerException("要删除的产品信息不存在");
		}
		productMapper.deleteByPrimaryKey(id);
	}
	@Override
	public List<ProductExtend> queryCascade(String name, String status, Long productCategoryId) {
		// TODO Auto-generated method stub
		return productExtendMapper.select(name, status, productCategoryId);
	}
	@Override
	public List<ProductExtend> pageQuery(int page, int pagesize, String name, String status, Long productCategoryId) {
		// TODO Auto-generated method stub
		int pageIndex=(page-1)*pagesize;
		List<ProductExtend>productExtends=productExtendMapper.pageQuery(pageIndex,pagesize, name, status,productCategoryId);
		return productExtends;
	}
	@Override
	public void online(Long id) {
		// TODO Auto-generated method stub
		Product product=productMapper.selectByPrimaryKey(id);
		product.setStatus("正常");
		productMapper.updateByPrimaryKey(product);
	}
	@Override
	public void offline(Long id) {
		// TODO Auto-generated method stub
		Product product=productMapper.selectByPrimaryKey(id);
		product.setStatus("下架");
		productMapper.updateByPrimaryKey(product);
	}
	
	
	
	
	

	
	
}
